package com.spzx.product.controller;

import com.spzx.common.core.web.domain.AjaxResult;
import com.spzx.common.core.web.page.TableDataInfo;
import com.spzx.common.security.utils.SecurityUtils;
import com.spzx.product.api.domain.Brand;
import com.spzx.product.domain.CategoryBrand;
import com.spzx.product.service.CategoryBrandService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.spzx.common.core.web.controller.BaseController;

import java.util.List;

/**
 * <p>
 * 分类品牌 前端控制器
 * </p>
 *
 * @author atguigu
 * @since 2024-10-26
 */
@Tag(name = "分类品牌")
@RestController
@RequestMapping("/categoryBrand")
public class CategoryBrandController extends BaseController {

    @Autowired
    private CategoryBrandService categoryBrandService;

    @Operation(description = "分页带条件查询")
    @GetMapping("/list")
    public TableDataInfo getPageList(CategoryBrand categoryBrand){
        //开启分页
        startPage();
        //调用cateBrandService中带条件查询的方法
        List<CategoryBrand> categoryBrandList =  categoryBrandService.getPageList(categoryBrand);
        return getDataTable(categoryBrandList);
    }

    @Operation(summary = "新增分类品牌")
    @PostMapping()
    public AjaxResult saveCategoryBrand(@RequestBody CategoryBrand categoryBrand){
        boolean save = categoryBrandService.saveCategoryBrand(categoryBrand);
        return toAjax(save);
    }

    @Operation(summary = "根据Id查询品牌信息")
    @GetMapping("/{id}")
    public AjaxResult getCategoryBrand(@PathVariable Long id){
        return AjaxResult.success(categoryBrandService.selectCategoryBrandById(id));
    }

    @Operation(summary = "根据Id修改品牌信息")
    @PutMapping
    public AjaxResult updateCategoryBrand(@RequestBody CategoryBrand categoryBrand){
        //设置更新者
        categoryBrand.setUpdateBy(SecurityUtils.getUsername());
        int update = categoryBrandService.updateCategoryBrandById(categoryBrand);
        return toAjax(update);
    }

    @Operation(summary = "根据Ids删除品牌信息")
    @DeleteMapping("/{ids}")
    public AjaxResult deleteCategoryBrand(@PathVariable List<Long> ids){
        boolean batchByIds = categoryBrandService.removeBatchByIds(ids);
        return toAjax(batchByIds);
    }

    @Operation(summary = "根据ID查询品牌列表")
    @GetMapping("/brandList/{id}")
    public AjaxResult getBrandListByCategoryId(@PathVariable Long id){
        List<Brand> list = categoryBrandService.getBrandListByCategoryId(id);
        return AjaxResult.success(list);
    }

}
